﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleBlog.Models;
using SimpleBlog.Infrastructure.Repositories;
using SimpleBlog.Infrastructure.Database;

namespace SimpleBlog.Infrastructure.Repositories
{
    public class ConfigurationRepository : Repository<Configuration>, IConfigurationRepository
    {
        protected override Func<Configuration, object> DefaultOrderBy
        {
            get
            {
                return x => null;
            }
        }

        private HashSet<Configuration> _CachedConfigurations;

        public ConfigurationRepository(SbContext context)
            : base(context)
        {
            _CachedConfigurations = new HashSet<Configuration>();
        }

        public string BlogTitle
        {
            get
            {
                return this["BlogTitle"]; ;
            }
            set
            {
                this["BlogTitle"] = value;
            }
        }

        public string BlogSubtitle
        {
            get
            {
                return this["BlogSubtitle"]; ;
            }
            set
            {
                this["BlogSubtitle"] = value;
            }
        }

        public string AboutTitle
        {
            get
            {
                return this["AboutTitle"]; ;
            }
            set
            {
                this["AboutTitle"] = value;
            }
        }

        public string AboutText
        {
            get
            {
                return this["AboutText"]; ;
            }
            set
            {
                this["AboutText"] = value;
            }
        }

        public string GoogleAnalyticsCode
        {
            get
            {
                return this["GoogleAnalyticsCode"]; ;
            }
            set
            {
                this["GoogleAnalyticsCode"] = value;
            }
        }
        
        public string ReCaptchaPublicKey
        {
            get
            {
                return this["ReCaptchaPublicKey"]; ;
            }
            set
            {
                this["ReCaptchaPublicKey"] = value;
            }
        }

        public string ReCaptchaPrivateKey
        {
            get
            {
                return this["ReCaptchaPrivateKey"]; ;
            }
            set
            {
                this["ReCaptchaPrivateKey"] = value;
            }
        }

        public string Footer
        {
            get
            {
                return this["Footer"]; ;
            }
            set
            {
                this["Footer"] = value;
            }
        }

        public string Copyright
        {
            get
            {
                return this["Copyright"]; ;
            }
            set
            {
                this["Copyright"] = value;
            }
        }

        public string this[string key]
        {
            get
            {
                Configuration config = _CachedConfigurations.FirstOrDefault(x => x.ConfigKey == key);
                if (config == null)
                {
                    config = this.FindByKey(key);
                    if (config != null)
                    {
                        _CachedConfigurations.Add(config);
                    }
                }
                return config != null ? config.ConfigValue : null;
            }
            set
            {
                var configuration = this.FindByKey(key);
                if (configuration != null)
                {
                    configuration.ConfigValue = value;
                    this.Modify(configuration);
                }
                else
                {
                    configuration = new Configuration() { ConfigKey = key, ConfigValue = value };
                    this.Add(configuration);
                }
                _CachedConfigurations.RemoveWhere(x => x.ConfigKey == key);
                var refreshConfiguration = this[key];
            }
        }

        public Configuration FindByKey(string key)
        {
            var configuration = this.Search(x => x.ConfigKey == key, 0, 1).FirstOrDefault();
            return configuration;
        }
    }
}